package com.fly.core.security.validate.code.config;

import com.fly.core.security.properties.SecurityProperties;
import com.fly.core.security.validate.code.imageCode.ImageCodeGenerator;
import com.fly.core.security.validate.code.ValidateCodeGenerator;
import com.fly.core.security.validate.code.smsCode.DefaultSmsCodeSender;
import com.fly.core.security.validate.code.smsCode.SmsCodeSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 验证码生成器配置
 * 如:用户配置imageCodeGenerator bean覆盖默认的图形验证码
 */
@Configuration
public class ValidateCodeBeanConfig {
    @Autowired
    private SecurityProperties securityProperties;

    // 图片验证码
    @Bean
    @ConditionalOnMissingBean(name = "imageCodeGenerator") //不存在imageCodeGenerator 的bean才使用此bean
    public ValidateCodeGenerator imageCodeGenerator(){
        ImageCodeGenerator codeGenerator = new ImageCodeGenerator();
        codeGenerator.setSecurityProperties(securityProperties);
        return codeGenerator;
    }
    //短信验证码
    @Bean
//    @ConditionalOnMissingBean(name = "smsCodeSender")
    @ConditionalOnMissingBean(SmsCodeSender.class) //写法二
    public SmsCodeSender smsCodeSender(){
        return new DefaultSmsCodeSender();
    }
}
